//复制到剪贴板
export const copyText = (text) => {
	if (navigator.clipboard) {
		// 使用现代的 navigator.clipboard API
		return navigator.clipboard.writeText(text).then(() => {
			console.log('复制成功');
		}).catch((error) => {
			console.error('复制失败:', error);
		});
	}
}
//深拷贝
export const deepCopy = obj => {
	let newobj = null;
	//判断是否需要继续进行递归
	if (typeof(obj) == 'object' && obj !== null) {
		newobj = obj instanceof Array ? [] : {}; //进行下一层递归克隆
		for (var i in obj) {
			newobj[i] = deepCopy(obj[i])
		} //如果不是对象直接赋值
	} else newobj = obj;
	return newobj;
}
/**
 * 数组排序,按数组中某一项排序
 * arr:被排序数组
 * attr:数组中需要排序的对象属性
 * rev:排序方式true:升序,false:降序
 */
export const arraySort = (arr, attr, rev = true) => {
	//
	if (attr) {
		function compare(attr, rev) {
			//第二个参数没有传递 默认升序排列
			if (rev === undefined) {
				rev = 1
			} else {
				rev = rev ? 1 : -1
			}
			return function(a, b) {
				a = a[attr]
				b = b[attr]
				if (a < b) {
					return rev * -1
				}
				if (a > b) {
					return rev * 1
				}
				return 0
			}
		}
		arr.sort(compare(attr, rev))
	} else {
		function compare(rev) {
			//第二个参数没有传递 默认升序排列
			if (rev === undefined) {
				rev = 1
			} else {
				rev = rev ? 1 : -1
			}
			return function(a, b) {
				if (a < b) {
					return rev * -1
				}
				if (a > b) {
					return rev * 1
				}
				return 0
			}
		}
		arr.sort(compare(rev))
	}
}
// 防抖
export const debounce = ((callback) => {
	let timer = null
	return (callback, wait = 800) => {
		timer && clearTimeout(timer)
		timer = setTimeout(callback, wait)
	}
})()
/**
 * 节流
 */
export const throttle = (() => {
	let last = 0
	return (callback, wait = 800) => {
		let now = +new Date()
		if (now - last > wait) {
			callback()
			last = now
		}
	}
})()
/**
 * 手机号脱敏
 */
export const hideMobile = (mobile) => {
	return mobile.toString().replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2")
}
/**
 * 全屏
 */
export const launchFullscreen = (element) => {
	if (element.requestFullscreen) {
		element.requestFullscreen()
	} else if (element.mozRequestFullScreen) {
		element.mozRequestFullScreen()
	} else if (element.msRequestFullscreen) {
		element.msRequestFullscreen()
	} else if (element.webkitRequestFullscreen) {
		element.webkitRequestFullScreen()
	}
}
/**
 * 关闭全屏
 */
export const exitFullscreen = () => {
	if (document.exitFullscreen) {
		document.exitFullscreen()
	} else if (document.msExitFullscreen) {
		document.msExitFullscreen()
	} else if (document.mozCancelFullScreen) {
		document.mozCancelFullScreen()
	} else if (document.webkitExitFullscreen) {
		document.webkitExitFullscreen()
	}
}
/**
 * 大小写转换
 * str 待转换的字符串
 * type 1-全小写 2-全大写 3-首字母大写
 */
export const turnCase = (str, type = 1) => {
	switch (type) {
		case 1:
			return str.toLowerCase()
		case 2:
			return str.toUpperCase()
		case 3:
			return str[0].toUpperCase() + str.substring(1).toLowerCase()
		default:
			return str
	}
}
/**
 * 数组对象根据字段去重
 * arr 要去重的数组
 * key 根据去重的字段名
 */
export const keyRepeat = (arr = [], key) => {
	if (arr.length === 0) return
	const map = new Map();
	return arr.filter(item => {
		if (!map.has(item[key])) {
			map.set(item[key], true);
			return true;
		}
		return false;
	});
}